package common;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import common.DiscussionSentence.MessageType;

public class Discussion {

	
	public static final int DIALOG_DISCUSSION = 0;
	public static final int WALL_DISCUSSION = 1;
	
	EasyChatEnvironment environment;
	private List<Locutor> locutors;
	private List<DiscussionSentence> sentences;
	private Locutor me = null;
	private DiscussionState state;
	private Object data = null;
	private String id;
	/**
	 * //DIALOG or WALL
	 */
	private int DiscussionType = 0; 
	
	public Discussion(EasyChatEnvironment easyChatEnvironment) {
		this.environment = easyChatEnvironment;
		locutors = new ArrayList<Locutor>();
		sentences = new ArrayList<DiscussionSentence>();
	}

	public void addLocutor(Locutor locutor) {
		locutors.add(locutor);
		if (me==null)
			me = locutor.getParentSet().getMe();
	}

	public boolean contains(Locutor locutor) {
		for (Locutor loc : getLocutors()) {
			if (loc.equals(locutor))
				return true;
		}
		return false;
	}

	public DiscussionSentence createSentence(Locutor locutor, String content,
			Date date,MessageType type) {
		
		DiscussionSentence sentence = new DiscussionSentence(content, date,
				this, locutor,type);
		return sentence;
	}

	@Override
	public boolean equals(Object obj) {

		if (obj instanceof Discussion) {
			if (((Discussion) obj).getLocutors().size() != getLocutors().size())
				return false;
			for (int i = 0; i < getLocutors().size(); i++) {
				if (!getLocutors().get(i).equals(
						((Discussion) obj).getLocutors().get(i)))
					return false;
			}
			return true;
		}
		return false;
	}

	public List<Locutor> getLocutors() {
		return locutors;
	}

	public List<DiscussionSentence> getSentences() {
		return sentences;
	}

	public DiscussionState getState() {
		return state;
	}

	public void setLocutors(List<Locutor> locutors) {
		this.locutors = locutors;
	}

	public void setSentences(List<DiscussionSentence> sentences) {
		this.sentences = sentences;
	}

	public void setState(DiscussionState state) {
		this.state = state;
	}

	public void setDiscussionType(int discussionType) {
		DiscussionType = discussionType;
	}

	public int getDiscussionType() {
		return DiscussionType;
	}

	public void setData(Object data) {
		this.data = data;
	}

	public Object getData() {
		return data;
	}

	public void setMe(Locutor me) {
		this.me = me;
	}

	public Locutor getMe() {
		return me;
	}

	public void clearNotifications() {
		int size = getSentences().size();
		for (int i=size-1;i>=0 ; i--) {
			if (sentences.get(i).getMessageType()==MessageType.NOTIFICATION)
				sentences.remove(i);
		}
		
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getId() {
		return id;
	}

}
